package com.mamengwei;

import com.mamengwei.mapper.DishMapper;
import com.mamengwei.mapper.EmployeeMapper;
import com.mamengwei.mapper.OrderMapper;
import com.mamengwei.pojo.Dish;
import com.mamengwei.pojo.Employee;
import com.mamengwei.pojo.Order;
import com.sun.org.apache.xpath.internal.operations.Or;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

import static org.apache.ibatis.io.Resources.getResourceAsStream;

/**
 * Hello world!
 *
 */
public class App {
    static Scanner scanner = new Scanner(System.in);
    public static void main( String[] args ) throws IOException {
        int flag = 1;
        System.out.println("漂亮团外卖系统");
        while (true){
            if(flag == 2){
                break;
            }
            System.out.println("1.商家登录");
            System.out.println("2.商家注册");
            System.out.println("3.退出系统");
            System.out.println("请输入操作命令:");

            int select = scanner.nextInt();
            switch (select){
                case 1:login();break;
                case 2:register();break;
                case 3:flag = 2;break;
            }
        }

    }
    static void login() throws IOException {
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        EmployeeMapper mapper = (EmployeeMapper)getMapper(EmployeeMapper.class);
        int i = mapper.login(new Employee(username,password));
        if(i>0){
            System.out.println("登录成功");
            logined();
        }else {
            System.out.println("用户名密码错误");
        }
    }
    static void register() throws IOException {
        System.out.println("请输入用户名:");
        String username = scanner.next();
        System.out.println("请输入密码:");
        String password = scanner.next();
        EmployeeMapper mapper = (EmployeeMapper)getMapper(EmployeeMapper.class);
        int isHaveUser = mapper.isHaveUser(username);
        if (isHaveUser>0){
            System.out.println("用户名已存在");
        }else {
            int i = mapper.register(new Employee(username, password));
            if(i>0){
                System.out.println("注册成功");
            }
        }
    }
    static void logined() throws IOException {
        System.out.println("欢迎来到商家页面");
        int flag = 1;

        while (true){
            if(flag == 2){
                break;
            }
            System.out.println("1.菜品管理");
            System.out.println("2.订单管理");
            System.out.println("3.推出商家管理页面");
            int select = scanner.nextInt();
            switch (select){
                case 1:dishManage();break;
                case 2:orderManage();break;
                case 3:flag = 2;break;
            }
        }
    }
    static void dishManage() throws IOException {
        System.out.println("欢迎来到菜品管理");
        int flag = 1;
        while (true){
            if(flag == 2){
                break;
            }
            System.out.println("1.查询菜品");
            System.out.println("2.增加菜品");
            System.out.println("3.删除菜品");
            System.out.println("4.修改菜品");
            System.out.println("5.退出菜品管理");
            System.out.println("请输入你的选择:");
            int select = scanner.nextInt();
            switch (select){
                case 1:showDish();break;
                case 2:addDish();break;
                case 3:deleteDish();break;
                case 4:updateDish();break;
                case 5:flag = 2;break;
            }
        }
    }
    static void showDish() throws IOException {
        DishMapper mapper = (DishMapper)getMapper(DishMapper.class);
        List<Dish> dishes = mapper.getDishes();
        for (Dish dish:
             dishes) {
            System.out.println("菜品ID:"+dish.getDish_id()+",菜品名称:"+dish.getName()+",菜品价格:"+dish.getPrice());
        }
    }
    static void addDish() throws IOException {
        DishMapper mapper = (DishMapper)getMapper(DishMapper.class);
        System.out.println("请输入要添加的菜名:");
        String name = scanner.next();
        System.out.println("请输入要添加菜的价格:");
        double price = scanner.nextDouble();
        int i = mapper.addDish(new Dish(name, price));
        if(i>0){
            System.out.println("添加成功");
        }else {
            System.out.println("添加失败");
        }
    }
    static void deleteDish() throws IOException {
        DishMapper mapper = (DishMapper)getMapper(DishMapper.class);
        showDish();
        System.out.println("请输入要删除菜品的id:");
        Long id = scanner.nextLong();
        int i = mapper.deleteDish(id);
        if(i>0){
            System.out.println("删除成功");
        }else {
            System.out.println("删除失败");
        }
    }

    static void updateDish() throws IOException {
        DishMapper mapper = (DishMapper)getMapper(DishMapper.class);
        showDish();
        System.out.println("请输入要修改的菜的ID:");
        Long id = scanner.nextLong();
        System.out.println("请输入要把菜的名字修改为:");
        String name = scanner.next();
        System.out.println("请输入要把菜的价格修改为:");
        double price = scanner.nextDouble();
        int i = mapper.updateDish(new Dish(id, name, price));
        if(i > 0){
            System.out.println("修改成功");
        }else{
            System.out.println("修改失败");
        }
    }
    static void orderManage() throws IOException {
        System.out.println("欢迎来到订单管理");
        int flag = 1;
        while (true){
            if(flag == 2){
                break;
            }
            System.out.println("1.查询订单");
            System.out.println("2.增加订单");
            System.out.println("3.删除订单");
            System.out.println("4.修改订单");
            System.out.println("5.退出订单管理");
            System.out.println("请输入你的选择:");
            int select = scanner.nextInt();
            switch (select){
                case 1:showOrder();break;
                case 2:addOrder();break;
                case 3:deleteOrder();break;
                case 4:updateOrder();break;
                case 5:flag = 2;break;
            }
        }
    }
    static void showOrder() throws IOException {
        OrderMapper orderMapper = (OrderMapper)getMapper(OrderMapper.class);
        List<Order> orderes = orderMapper.getOrderes();
        for (Order order:
             orderes) {
            System.out.println("订单ID:"+order.getId()+"  订单人:"+order.getShopName()+"  订单地址"+order.getAddress()+"  订单时间:"+order.getOrderTime());
        }
    }
    static void addOrder() throws IOException {
        OrderMapper orderMapper = (OrderMapper)getMapper(OrderMapper.class);
        System.out.println("请输入要添加的订单人:");
        String name = scanner.next();
        System.out.println("请输入要添加的订单地址:");
        String address = scanner.next();
        int i = orderMapper.addOrder(new Order(name, address, new Date()));
        if(i>0){
            System.out.println("添加订单成功");
        }else {
            System.out.println("添加订单失败");
        }
    }
    static void deleteOrder() throws IOException {
        showOrder();
        OrderMapper orderMapper = (OrderMapper)getMapper(OrderMapper.class);
        System.out.println("请输入订单id");
        Long id = scanner.nextLong();
        int i = orderMapper.deleteOrder(id);
        if(i>0){
            System.out.println("删除订单成功");
        }
    }
    static void updateOrder() throws IOException {
        showOrder();
        OrderMapper orderMapper = (OrderMapper)getMapper(OrderMapper.class);
        System.out.println("请输入要更改订单的ID:");
        long id = scanner.nextLong();
        System.out.println("请输入把该订单的订单人修改为:");
        String name = scanner.next();
        System.out.println("请输入把该订单的地址修改为:");
        String address = scanner.next();
        int i = orderMapper.updateOrder(new Order(id, name, address,new Date()));
        if(i>0){
            System.out.println("修改成功");
        }
    }


    static Object getMapper(Class clas) throws IOException {

        InputStream is = getResourceAsStream("mybatis-config.xml");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        Object mapper = sqlSession.getMapper(clas);
        return mapper;
    }

}
/*
//        Object mapper = getMapper(EmployeeMapper.class);
//        EmployeeMapper employeeMapper = (EmployeeMapper)mapper;
//        List<Employee> employees = employeeMapper.getEmployees();
//        for (Employee emp:
//             employees) {
//            System.out.println("员工ID:"+emp.getId()+",员工用户名:"+emp.getUsername());
//        }
 */